package com.ruoyi.exam.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.AddressUtils;
import com.ruoyi.common.utils.ip.IpUtils;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.exam.domain.entity.SafetyActivity;
import com.ruoyi.exam.service.ISafetyActivityService;
import com.ruoyi.system.domain.SysOperLog;
import com.ruoyi.system.service.ISysOperLogService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 班组安全活动记录，用于记录班组的安全活动信息Controller
 * 
 * @author ruoyi
 * @date 2024-12-17
 */
@RestController
@RequestMapping("/exam/activity")
public class SafetyActivityController extends BaseController {
    @Autowired
    private ISafetyActivityService safetyActivityService;

    @Autowired
    private ISysOperLogService sysOperLogService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询班组安全活动记录，用于记录班组的安全活动信息列表
     */
    @PreAuthorize("@ss.hasPermi('exam:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(SafetyActivity safetyActivity) {
        startPage();
        List<SafetyActivity> list = safetyActivityService.selectSafetyActivityList(safetyActivity);
        return getDataTable(list);
    }

    /**
     * 导出班组安全活动记录，用于记录班组的安全活动信息列表
     */
    @PreAuthorize("@ss.hasPermi('exam:activity:export')")
    @Log(title = "班组安全活动记录，用于记录班组的安全活动信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SafetyActivity safetyActivity) {
        List<SafetyActivity> list = safetyActivityService.selectSafetyActivityList(safetyActivity);
        ExcelUtil<SafetyActivity> util = new ExcelUtil<SafetyActivity>(SafetyActivity.class);
        util.exportExcel(response, list, "班组安全活动记录，用于记录班组的安全活动信息数据");
    }

    /**
     * 获取班组安全活动记录，用于记录班组的安全活动信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('exam:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(safetyActivityService.selectSafetyActivityById(id));
    }

    /**
     * 获取班组安全活动记录，用于记录班组的安全活动信息详细信息
     */
    @GetMapping(value = "/view/{id}")
    // @Log(title = "班组安全活动查阅", businessType = BusinessType.INSERT)
    public AjaxResult view(@PathVariable("id") Long id) {

        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();

        // *========数据库日志=========*//
        SysOperLog operLog = new SysOperLog();
        operLog.setTitle("班组安全活动查阅");
        operLog.setBusinessType(1);
        operLog.setMethod("com.ruoyi.exam.controller.SafetyActivityController.view()");
        operLog.setRequestMethod("GET");
        operLog.setOperatorType(1);
        operLog.setStatus(0);
        // 请求的地址
        String ip = IpUtils.getIpAddr();
        operLog.setOperIp(ip);
        operLog.setOperUrl("/exam/activity/view/" + id);
        operLog.setOperLocation(AddressUtils.getRealAddressByIP(operLog.getOperIp()));
        if (loginUser != null) {
            operLog.setOperName(loginUser.getUsername());
            SysUser currentUser = loginUser.getUser();
            if (StringUtils.isNotNull(currentUser) && StringUtils.isNotNull(currentUser.getDept())) {
                operLog.setDeptName(currentUser.getDept().getDeptName());
            }
        }
        sysOperLogService.insertOperlog(operLog);

        SysOperLog sysOperLog = new SysOperLog();
        sysOperLog.setTitle("班组安全活动查阅");
        sysOperLog.setOperUrl("/exam/activity/view/" + id);
        List<SysOperLog> selectOperLogList = sysOperLogService.selectOperLogList(sysOperLog);
        // for 循环遍历tActivityRecordsList
        for (SysOperLog sysOperLog2 : selectOperLogList) {
            sysOperLog2.setOperName(
                    sysUserService.selectUserByUserName(sysOperLog2.getOperName()).getNickName() + "（"
                            + sysOperLog2.getOperName() + "）");
        }
        return success(selectOperLogList);
    }

    /**
     * 新增班组安全活动记录，用于记录班组的安全活动信息
     */
    @PreAuthorize("@ss.hasPermi('exam:activity:add')")
    @Log(title = "班组安全活动记录，用于记录班组的安全活动信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SafetyActivity safetyActivity) {
        safetyActivity.setDeptId(SecurityUtils.getLoginUser().getDeptId());
        return toAjax(safetyActivityService.insertSafetyActivity(safetyActivity));
    }

    /**
     * 修改班组安全活动记录，用于记录班组的安全活动信息
     */
    @PreAuthorize("@ss.hasPermi('exam:activity:edit')")
    @Log(title = "班组安全活动记录，用于记录班组的安全活动信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SafetyActivity safetyActivity) {
        return toAjax(safetyActivityService.updateSafetyActivity(safetyActivity));
    }

    /**
     * 删除班组安全活动记录，用于记录班组的安全活动信息
     */
    @PreAuthorize("@ss.hasPermi('exam:activity:remove')")
    @Log(title = "班组安全活动记录，用于记录班组的安全活动信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(safetyActivityService.deleteSafetyActivityByIds(ids));
    }
}
